package com.qiaofang.applet.service.other.service.impl;


import com.qiaofang.applet.common.base.MongoBaseService;
import com.qiaofang.applet.common.exception.BusinessException;
import com.qiaofang.applet.common.util.UuidUtil;
import com.qiaofang.applet.service.member.domain.Employee;
import com.qiaofang.applet.service.other.domain.Corporation;
import com.qiaofang.applet.service.other.domain.EmployeeLabel;
import com.qiaofang.applet.service.other.repository.CorporationRepository;
import com.qiaofang.applet.service.other.service.EmployeeLabelService;
import com.qiaofang.applet.service.other.service.mapstruct.EmployeeLabelMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 经纪人标签管理接口实现
 * @author Administrator
 */
@Slf4j
@Service
public class EmployeeLabelServiceImpl extends MongoBaseService<Corporation,String> implements EmployeeLabelService {

    private final CorporationRepository repository;
    

    @Autowired
    private MongoTemplate mongoTemplate;


    @Autowired
    public EmployeeLabelServiceImpl(CorporationRepository repository) {
        super(repository);
        this.repository = repository;
    }

    /**
     * 查询集团所有经纪人标签
     *
     * @param corporationCode
     * @return
     */
    @Override
    public List<EmployeeLabel> findAllEmployeeLabel(String corporationCode) {
        Corporation corporation = repository.findOneByCorporationCode(corporationCode);
        if (corporation == null) {
            return new ArrayList<>();
        }
        List<EmployeeLabel> employeeLabels = corporation.getEmployeeLabelList();
        return employeeLabels;
    }

    /**
     * 保存经纪人标签
     *
     * @param employeeLabel
     * @param corporationCode
     */
    @Override
    public void saveEmployeeLabel(EmployeeLabel employeeLabel, String corporationCode) {
        Corporation corporation = repository.findOneByCorporationCode(corporationCode);
        if (corporation == null) {
            throw new BusinessException("集团不存在");
        }
        //获取集团的经纪人标签List
        List<EmployeeLabel> employeeLabels = corporation.getEmployeeLabelList();

        //判断传入参数是否有标签Uuid，有则update无则insert
        if(StringUtils.isEmpty(employeeLabel.getLabelUuid())){
            employeeLabel.setLabelUuid(UuidUtil.geneUuidString());
            employeeLabels.add(employeeLabel);
        }else {
            employeeLabels.forEach(label -> {
                if (employeeLabel.getLabelUuid().equals(label.getLabelUuid())) {
                    label.setLabelContent(employeeLabel.getLabelContent());
                }
            });
        }
        Set<String> contentSet = employeeLabels.stream().map(EmployeeLabel::getLabelContent).collect(Collectors.toSet());
        if (contentSet.size() < employeeLabels.size()){
            throw new BusinessException("该标签已存在");
        }
        corporation.setEmployeeLabelList(employeeLabels);
        repository.save(corporation);
    }

    /**
     * 删除除经纪人标签
     *
     * @param labelUuid
     * @param corporationCode
     */
    @Override
    public void removeEmployeeLabel(String labelUuid, String corporationCode) {
        //先判断是否有经纪人使用
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and("labelUuidList").in(labelUuid);
        query.addCriteria(criteria);
        long count = mongoTemplate.count(query, Employee.class);
        if(count>0){
            throw new BusinessException("此标签已有经纪人使用");
        }

        //无经纪人使用则执行以下代码
        Corporation corporation = repository.findOneByCorporationCode(corporationCode);
        if (corporation == null) {
            throw new BusinessException("集团不存在");
        }

        List<EmployeeLabel> employeeLabels = corporation.getEmployeeLabelList();

        //遍历删除对应的标签
        Iterator<EmployeeLabel> labelIterator = employeeLabels.iterator();
        while (labelIterator.hasNext()){
            EmployeeLabel label = labelIterator.next();
            if(label.getLabelUuid().equals(labelUuid)){
                labelIterator.remove();
            }
        }
        //保存
        corporation.setEmployeeLabelList(employeeLabels);
        repository.save(corporation);

    }
}
